﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;

namespace RAD.LightOne
{
    #region Documentation
    /// <summary>
    /// Classe que representa uma tabela, ou seja, uma lista de campos.
    /// Uma tabela é, em si uma coleção de campos e seus valores.
    /// </summary>
    #endregion Documentation
    internal sealed class Table : ICollection<Field>, ICloneable
    {
        #region Fields
        private int index;

        private string name;

        private List<Field> indexedFields;
        private Dictionary<string,Field> namedFields;

        #endregion Fields

        #region Properties

        internal int Index
        {
            get
            {
                return this.index;
            }
            set
            {
                this.index = value;
            }
        }

        public string Name
        {
            get
            {
                return this.name;
            }
        }

         [System.Runtime.CompilerServices.IndexerName("GetItem")]
        public Field this[int index]
        {
            get
            {
                return this.indexedFields[index];
            }
        }

         [System.Runtime.CompilerServices.IndexerName("GetItem")]
        public Field this[string key]
        {
            get
            {
                return this.namedFields[key];
            }
        }

        #endregion Properties

        #region Constructros

        public Table(string name)
        {
            if (name == null || name == string.Empty) throw new ArgumentNullException();
            this.name = name;
            this.indexedFields = new List<Field>();
            this.namedFields = new Dictionary<string, Field>();
        }

        #endregion Constructors

        #region Methods

        public override string ToString()
        {
            return this.Name;
        }
        #endregion Methods

        #region ICollection<DBFieldItem> Members

        public void Add(Field item)
        {
            lock (this.indexedFields)
            {
                this.indexedFields.Add(item);
                this.namedFields.Add(item.Name, item);
                item.Index = this.indexedFields.IndexOf(item);
            }
        }

        #region Documentation
        /// <summary>
        /// Adiciona um array, lista ou que implemente IEnumerable à lista de uma só vez.
        /// </summary>
        /// <param name="collection">coleção de DBFieldItem a ser adicionada.</param>
        #endregion
        public void AddRange(IEnumerable<Field> collection)
        {
            this.indexedFields.AddRange(collection);
            foreach (Field item in collection)
            {
                this.namedFields.Add(item.Name, item);
                item.Index = this.indexedFields.IndexOf(item);
            }
        }

        public void Clear()
        {
            lock (this.indexedFields)
            {
                this.indexedFields.Clear();
                this.namedFields.Clear();
            }
        }

        public bool Contains(Field item)
        {
            return this.indexedFields.Contains(item);
        }

        public void CopyTo(Field[] array, int arrayIndex)
        {
            this.indexedFields.CopyTo(array, arrayIndex);
        }

        public void CopyTo(Field[] array)
        {
            this.indexedFields.CopyTo(array);
        }

        public Field[] ToArray()
        {
            return this.indexedFields.ToArray();
        }

        public bool IsReadOnly
        {
            get
            {
                return true;
            }
        }

        public bool Remove(Field item)
        {
            lock (indexedFields)
            {
                this.namedFields.Remove(item.Name);
                return this.indexedFields.Remove(item);
            }
        }

        public void RemoveAt(int index)
        {
            if (index <= this.indexedFields.Count)
            {
                lock (this.indexedFields)
                {
                    this.namedFields.Remove(this.indexedFields[index].Name);
                    this.indexedFields.RemoveAt(index);
                }
            }
            else
            {
                throw new ArgumentOutOfRangeException("Index less than 0(zero) or upper than list.count.");
            }
        }

        #endregion

        #region ICollection<DBFieldItem> Members


        public int Count
        {
            get { return this.indexedFields.Count; }
        }

        #endregion

        #region IEnumerable Members

        public IEnumerator GetEnumerator()
        {
            return this.indexedFields.GetEnumerator();
        }

        #endregion

        #region IEnumerable<DBFieldItem> Members

        IEnumerator<Field> IEnumerable<Field>.GetEnumerator()
        {
            return this.indexedFields.GetEnumerator();
        }

        #endregion

        #region ICloneable Members

        public object Clone()
        {
            object newObject = this.MemberwiseClone() as object;
            return newObject;
        }

        #endregion
    }
}
